<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      default-expand-all
      check-strictly
      node-key="id"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoleDetail, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import { arrayToTree } from '@/utils'
export default {
  props: {
    roleId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermission()
    this.loadPermissionByRoleId()
  },
  methods: {
    async hSubmit() {
    // 1.组装参数
      const permIds = this.$refs.tree.getCheckedKeys() // 当前用户选中id
      console.log('permIds', permIds)
      // 2. 调用api
      await assignPerm({ id: this.roleId, permIds: permIds })
      // 3. 关闭弹框
      this.$emit('success')
    },
    // 获取基本信息,
    async loadPermission() {
      const res = await getPermissionList()
      console.log(res)
      this.list = arrayToTree(res.data)
    },
    async loadPermissionByRoleId() {
      const res = await getRoleDetail(this.roleId)
      console.log('res', res)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      res.data.permIds
    }
  }
}
</script>
